﻿using DAL;
using IBLL;
using IDAL;
using MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL
{
   public class ProductBLL:BaseBLL<Product, ProductDAL>, IProductBLL
    {
        IProductSkuDAL skuDal = new ProductSkuDAL();
        IProductAttrDAL attrDal = new ProductAttrDAL();
        public int Add(Product product, List<ProductSku> skuList, List<ProductAttr> attrList) {
            int result = 0;
            var tran = dal.BeginTran();
            try
            {
                //1.插入商品表(product表)
                dal.Add(product);
                result += SaveChange();
                //侯海啸
                //2.插入productsku表
                foreach (var sku in skuList)
                {
                    sku.ProductID = product.ID;
                    skuDal.Add(sku);
                }
                //3.插入productattr表
                foreach (var attr in attrList)
                {
                    attr.ProductID = product.ID;
                    attrDal.Add(attr);
                }
                result += SaveChange();
                tran.Commit();
            }
            catch (Exception)
            {
                tran.Rollback();
            }
           
           return result;
        }
        
        public int Update(Product product, List<ProductSku> skuList, List<ProductAttr> attrList)
        {
            //首先修改商品表（Product表）
            product.UpdateTime = DateTime.Now;
            dal.Update(product);

            //2 对ProductSku和ProductAttr的修改，分两步，首先是删除全部数据，再重新添加
            //ProductSku表
            var skus = skuDal.Search(x => x.ProductID == product.ID);
            foreach (var item in skus)
            {
                skuDal.Delete(item);//对ProductSku的Delete语句
            }
            foreach (var sku in skuList)
            {
                sku.ProductID = product.ID;//对ProductSku的Add语句
                skuDal.Add(sku);
            };

            //ProductAttr表
            var attrs = attrDal.Search(x => x.ProductID == product.ID);
            foreach (var item in attrs)
            {
                attrDal.Delete(item);//对ProductSku的Delete语句
            }
            foreach (var attr in attrList)
            {
                attr.ProductID = product.ID;//对ProductAttr的Add语句
                attrDal.Add(attr);
            };

            //提交事务
            return SaveChange();
        }
        public override int Delete(int id)
        {
            //删除
            //侯海啸
            
            dal.Delete(id);
            var skus = skuDal.Search(x => x.ProductID == id);
            foreach (var sku in skus) {
                skuDal.Delete(sku);
            }
            var attrs = attrDal.Search(x => x.ProductID == id);
            foreach (var attr in attrs) {
                attrDal.Delete(attr);
            }
            int result = SaveChange();
            return result;
        }

        public  Product GetOne(int id,out List<ProductAttr> attrs, out List<ProductSku> skus)
        {
            //默认情况下c#方法返回值只有一个，为了弥补这种缺陷，应使用out参数变相增加返回值
            //out参数:必须在方法体内为其赋值

            var product = dal.GetOne(id);
             skus = skuDal.Search(x => x.ProductID == id);
             attrs = attrDal.Search(x => x.ProductID == id);

            return product;
        }
    }
}
